package com.kgd.news.ibeetl.func;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.kgd.news.utils.Constants;
import org.beetl.core.Context;
import org.beetl.core.Function;

import java.util.List;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/8/3
 * @Time: 下午2:47
 */
public class OneToManyDisplayValueFunc implements Function {

    @Override
    public Object call(Object[] objects, Context context) {
        try {
            if (objects.length != 3) {
                return "参数错误";
            }
            String id = objects[0] + "";
            String tblName = objects[1] + "";
            String colName = objects[2] + "";
//            这个是自动的，有缓存就去用缓存，没有就去查最新的吧，这个更方便，比encatch更方便吧
            Record record=   Db.findFirstByCache(Constants.dict_func_cacheName, "displayvaluefunc"+id,"select  parentid  from  category  where id = ?", id);
            Object object = record.get("parentid");
            List<Record> data = Db.findByCache(Constants.dict_func_cacheName,"displayvaluefunclist"+object,"select " + colName + " from " + tblName + " where d_group =\"news\" and  d_value = ?", object);
            if (data == null && data.isEmpty()) {
                return "无";
            } else {
                return data.get(0).get(colName);
            }
        } catch (Exception e) {
            return "暂无";
        }
    }
}
